package x.y.z;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jackson.JacksonProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import x.y.z.properties.Config1;
//import x.y.z.properties.Config1;
import x.y.z.properties.Config2;
import x.y.z.properties.Config3;
import x.y.z.properties.Config4;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
@Controller
//@EnableConfigurationProperties({Config3.class})
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }


    @Autowired
    Config1 cfg1;
    @Autowired
    Config2 cfg2;
    @Autowired
    Config3 cfg3;
    @Autowired
    Config4 cfg4;

//    @Value("${app.name}")
//    String val ;

    @GetMapping("")
    @ResponseBody
    public Object i0(){
        Map<String,Object> m = new HashMap<>();
        m.put("1",cfg1);
        m.put("2",cfg2);
        m.put("3",cfg3);
        m.put("4",cfg4);
        return m;
    }

    @PostConstruct
    public void ii() throws JsonProcessingException {

//        Assert.isTrue(new File("application.yml").exists(),"文件不存在");


//        System.out.println(mapper.writeValueAsString(cfg3));
//        System.out.println(cfg1.getName());
//        System.out.println(cfg3.getName());
    }
}
@Component
class Runner1 implements CommandLineRunner{

    @Autowired
    Config1 cfg1;
    @Autowired
    Config2 cfg2;
    @Autowired
    Config3 cfg3;
    @Autowired
    Config4 cfg4;

    @Resource
    JacksonProperties properties;

    @Override
    public void run(String... args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writeValueAsString(cfg1));
        System.out.println(mapper.writeValueAsString(cfg2));
        System.out.println(mapper.writeValueAsString(cfg3));
        System.out.println(mapper.writeValueAsString(cfg4));
        System.out.println(mapper.writeValueAsString(properties));
    }
}
